import {
  BeforeInsert,
  Column,
  CreateDateColumn,
  Entity,
  PrimaryGeneratedColumn,
  UpdateDateColumn,
} from 'typeorm';

// 用户表
// 表名：user
@Entity({ name: 'user' })
export class User {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ type: 'varchar', length: 20 })
  username: string;
  @Column({
    type: 'json',
    nullable: true,
  })
  role: string[];
  @Column({ nullable: true })
  departId: number;

  @Column()
  password: string;

  @Column({ nullable: true })
  email: string;

  @Column({ default: true })
  isActive: boolean;

  @CreateDateColumn({
    type: 'timestamp',
    comment: '创建时间',
    nullable: true,
  })
  createdAt: Date;

  @UpdateDateColumn({
    type: 'timestamp',
    comment: '更新时间',
    nullable: true,
  })
  updatedAt: Date;

  @BeforeInsert()
  setDefaultRole() {
    if (!this.role) {
      this.role = ['user'];
    }
  }
  @BeforeInsert()
  setDefaultDepartId() {
    if (!this.departId) {
      this.departId = 1;
    }
  }
}
